package com.jeeplus.test.jlnu.teachingArchive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.AcademicInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.InsideInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.vo.InsideInfoVo;
import com.jeeplus.test.jlnu.teachingArchive.mapper.InsideInfoMapper;
import com.jeeplus.test.jlnu.teachingArchive.service.InsideInfoService;
import com.jeeplus.test.tools.resp.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class InsideInfoServiceImpl extends ServiceImpl<InsideInfoMapper,InsideInfo> implements InsideInfoService {
    @Autowired
    private InsideInfoMapper insideInfoMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveAndLinkAcademic(InsideInfoVo insideInfoVo) {
        if (insideInfoVo.getIds() == null || insideInfoVo.getIds().trim().isEmpty()) {
            return R.error("9999", "ids参数不能为空");
        }

        List<String> idList = Arrays.asList(insideInfoVo.getIds().split(","));
        if (CollectionUtils.isEmpty(idList)) {
            return R.error("9999", "ids解析后为空");
        }

        InsideInfo insideInfo = new InsideInfo();
        BeanUtils.copyProperties(insideInfoVo, insideInfo);

        boolean saveSuccess = save(insideInfo);
        if (!saveSuccess) {
            return R.error("9999", "新增内部信息失败");
        }

        String insideId = insideInfo.getId();

        int updateCount = baseMapper.batchUpdateAcademicInsideId(insideId, idList);
        if (updateCount <= 0) {
            return R.suc("内部信息新增成功，但未更新到另一张表");
        }

        return R.suc("操作成功");
    }
    @Override
    public InsideInfoVo getDetailById(String id) {
        InsideInfoVo detailVo = new InsideInfoVo();
        InsideInfo insideInfo = insideInfoMapper.selectById(id);
        if (insideInfo == null) {
            return detailVo;
        }
        detailVo.setInsideInfo(insideInfo);

        List<AcademicInfo> academicList = insideInfoMapper.selectAcademicListByInsideId(id);
        detailVo.setAcademicInfoList(academicList);

        return detailVo;
    }
    @Override
    public InsideInfoVo saveOrUpdate(InsideInfoVo vo) {
        InsideInfo insideInfo = new InsideInfo();
        BeanUtils.copyProperties(vo, insideInfo);

        boolean isSuccess = super.saveOrUpdate(insideInfo);
        if (!isSuccess) {
            return null;
        }

        InsideInfoVo resultVo = new InsideInfoVo();
        resultVo.setInsideInfo(insideInfo);

        return resultVo;
    }
    @Override
    public IPage<InsideInfoVo> page(Page<InsideInfo> page, InsideInfo query) {
        QueryWrapper<InsideInfo> wrapper = new QueryWrapper<>(query)
                .orderByDesc("form_date");

        IPage<InsideInfo> entityPage = baseMapper.selectPage(page, wrapper);

        IPage<InsideInfoVo> voPage = new Page<>(
                entityPage.getCurrent(),
                entityPage.getSize(),
                entityPage.getTotal()
        );

        List<InsideInfoVo> voList = entityPage.getRecords().stream().map(insideInfo -> {
            InsideInfoVo vo = new InsideInfoVo();
            vo.setInsideInfo(insideInfo);
            return vo;
        }).collect(Collectors.toList());

        voPage.setRecords(voList);
        return voPage;
    }

}
